package com.tt.vtg.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.tt.vtg.dao.UserDao;
import com.tt.vtg.dao.entity.User;
import com.tt.vtg.plain.info.UserInfo;
import com.tt.vtg.service.UserService;

@Component("userSvc")
@Transactional(value = "txManager")
public class UserServiceImpl implements UserService{
	@Autowired
	private UserDao userDao;
	
	@Override
	public UserInfo get(Long id) {
		return new UserInfo(userDao.findOne(id));
	}

	@Override
	public void save(User t) {
		userDao.save(t);
	}

	@Override
	public void update(User t) {
		userDao.saveAndFlush(t);
	}

	@Override
	public UserInfo getById(String userId) {
		return new UserInfo(userDao.findByUserId(userId));
	}

	@Override
	public void delete(long id) {
		userDao.delete(id);
	}

	@Override
	public List<UserInfo> getAll() {
		return userDao.findAll().stream().map(u -> new UserInfo(u)).collect(Collectors.toCollection(ArrayList::new));
	}

	@Override
	public Optional<User> getUserEntityById(String userId) {
		return Optional.ofNullable(userDao.findByUserId(userId));
	}

	@Override
	public User findByUserName(String userName) {
		return userDao.findByUserName(userName);
	}

	@Override
	public Set<String> findRolesByUserName(String userName) {
		return userDao.findRolesByUserName(userName);
	}

	@Override
	public Set<String> findPermissionsByUserName(String userName) {
		return userDao.findPermissionsByUserName(userName);
	}

	@Override
	public User findOne(Long id) {
		return userDao.findOne(id);
	}


}
